<?php

namespace App\Http\Controllers;

use App\Models\FocusUser;
use App\Models\MessageResource;
use App\Models\Messages;
use Illuminate\Http\Request;

class MessageController extends Controller
{
    protected $request;
    protected $redis;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->redis = new \Predis\Client('127.0.0.1:6379');
    }

    public function getFocusList()
    {
        $stdData = array();
        $userId = $this->request->post('user_id', 0);

        // 获取关注用户列表
        $focusUser = new FocusUser();
        $focusInfo = $focusUser->getFocusList($userId);
        $stdData = array('focusList' => $focusInfo);
        return json_encode(['code' => 200, 'msg' => 'success', 'data' => $stdData]);
    }

    public function getMessagesList()
    {
        $stdData = array();
        $userId = $this->request->post('user_id', 0);

        $page = $this->request->post('page', 1);
        $size = $this->request->post('size', 5);

        $messages = new Messages();
        $messagesList = $messages->getMessagesList($userId, $page, $size);
        foreach($messagesList as $k => $v)
        {
            // 获取资源
            $messageResource = new MessageResource();
            $resourceList = $messageResource->getResourceList($v['id']);
            $messagesList[$k]['resourceList'] = $resourceList;

            // 计算时间
            $commentTime = self::timeExec($v['created_at']);
            $messagesList[$k]['commnetTime'] = $commentTime;
        }
        $stdData = $messagesList;
        return json_encode(['code' => 200, 'msg' => 'success', 'data' => $stdData]);
    }

    private function timeExec($time)
    {

        $diffTime = time() - strtotime($time);
        if($diffTime <= 5)
        {
            $msg = '刚刚';
        }
        elseif($diffTime > 5 && $diffTime <= 300)
        {
            $msg = '5秒前';
        }
        elseif($diffTime > 500 && $diffTime <= (3600 * 2))
        {
            $msg = '5分钟前';
        }
        elseif($diffTime > (3600 * 2) && $diffTime <= (86400 * 3))
        {
            $msg = '2小时前';
        }
        else
        {
            $msg = $time;
        }
        return $msg;
    }

    public function testMongo()
    {
        // 链接mongo
        $mongoConnect = new \MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');

        $bulk = new \MongoDB\Driver\BulkWrite; //默认是有序的，串行执行
        // 添加
        $bulk->insert(['user_id' => 2, 'real_name'=>'中国',]);
        $bulk->insert(['user_id' => 3, 'real_name'=>'中国人',]);
        $mongoConnect->executeBulkWrite('location2.box2', $bulk); //执行写入 location数据库下的box集合

        // 查询
        $filter =  ['user_id'=>['$gt'=>0]]; //查询条件 user_id大于0
        $options = [
            'projection' => ['_id' => 0], //不输出_id字段
            'sort' => ['user_id'=>-1] //根据user_id字段排序 1是升序，-1是降序
        ];
        $query = new \MongoDB\Driver\Query($filter, $options); //查询请求
        $list = $mongoConnect->executeQuery('location2.box2',$query); // 执行查询 location数据库下的box集合

        foreach ($list as $document) {
            // 对象
//            print_r($document);
            // 对象转数组
            $document = json_decode(json_encode($document), true);
            print_r($document);
        }
    }

    // 这是本地演示冲突

    // 为了演示冲突
}
